import { ref, unref, computed, onBeforeUnmount } from 'vue';
import api from '@/api';
import { message } from 'ant-design-vue';
import { useMessage } from '@/hooks/common/useMessage';
export const useSyncRequest = (loading, setLoading, emit: any = null) => {
    const { createJiraLogin } = useMessage();
    const loadingTip = ref<string>('');
    const timer = ref<any>(null);
    const loadingObj = computed(() => {
        const obj = {
            tip: unref(loadingTip),
            loading: loading.value
        };
        return loading.value ? obj : false;
    });
    const setLoadingTip = (val) => {
        loadingTip.value = val;
    };

    const syncDataFromJira = async (request, query, callback: any = null) => {
        setLoading(true);
        setLoadingTip('在数据量较大（超过1w条）时，该同步可能需要花费数十分钟，请耐心等待，同步期间请勿刷新页面');
        request(query)
            .then(({ success, errorCode, errorMsg }) => {
                if (success) {
                    getJiraStatus(query.systemId, callback);
                    timer.value = setInterval(() => {
                        getJiraStatus(query.systemId, callback);
                    }, 10 * 3000);
                } else {
                    if (errorCode === '003') {
                        setLoading(false);
                        createJiraLogin({
                            systemId: query.systemId,
                            onOk() {
                                callback && callback();
                            }
                        });
                        return;
                    }
                    setLoading(false);
                    message.error(errorMsg);
                }
            });
    };

    // 获取状态
    const getJiraStatus = async (id, callback) => {
        const query = {
            id
        };
        const { success, moule, errorMsg } = await api.jiraProjectModel.findSyncInfoBySystemId(query);
        if (success) {
            if (moule.syncStatus === '0') {
                if (timer.value) {
                    clearInterval(timer.value);
                    timer.value = null;
                }
                setLoading(false);
                setLoadingTip('');
                callback();
                if (emit) {
                    emit('success');
                }
            }
            if (moule.syncStatus === '2') {
                if (timer.value) {
                    clearInterval(timer.value);
                    timer.value = null;
                }
                message.error(moule.syncMessage);
                setLoading(false);
                setLoadingTip('');
            }
        } else {
            message.error(errorMsg);
            if (timer.value) {
                clearInterval(timer.value);
                timer.value = null;
            }
        }
    };
    // 离开页面前，取消定时器
    onBeforeUnmount(() => {
        if (timer.value) {
            clearInterval(timer.value);
            timer.value = null;
        }
    });
    return {
        loadingObj,
        setLoadingTip,
        getJiraStatus,
        syncDataFromJira
    };
};